package com.vaccine.controller;

import cn.hutool.core.util.StrUtil;
import com.vaccine.common.Result;
import com.vaccine.entity.VaccineOrder;
import com.vaccine.mapper.VaccineOrderMapper;
import com.vaccine.service.VaccineOrderService;
import com.vaccine.service.VaccineService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/vaccine_order")
public class VaccineOrderController {
    @Autowired
    private VaccineOrderService vaccineOrderService;
    @Autowired
    private VaccineService vaccineService;
    @Autowired
    private VaccineOrderMapper vaccineOrderMapper;

    @PostMapping("/insert/{id}")
    public Result<?> insert(@RequestBody VaccineOrder vaccineOrder, @PathVariable("id")Integer vaccineId){
        try {
            VaccineOrder order =vaccineOrderService.selectByUserId(vaccineOrder.getUserId());
            if (order==null||order.getStatus()!=3){
                synchronized (VaccineOrderController.class) {
                    vaccineOrderService.inserVaccineOrder(vaccineOrder);
                    Integer count = vaccineService.reduceCount(vaccineId);
                    return Result.success(count);
                }
            }else {
                return Result.error("1","当前用户已存在预约信息,不可重复预约");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("0","新增失败！");
        }
    }

    @GetMapping("/selectByUserId/{userId}")
    public Result<?> selectByUserId(@PathVariable("userId")Integer userId){
        VaccineOrder vaccineOrder = vaccineOrderService.selectByUserId(userId);
        if (vaccineOrder==null){
            return Result.error("1","没有该用户的疫苗预约信息");
        }
        return Result.success(vaccineOrder);
    }

    @GetMapping("/updateStatus")
    public Result<?> updateStatus(@RequestParam("id")Integer id,@RequestParam("status")Integer status){
        Integer flag = vaccineOrderService.updateStatus(id, status);
        if (flag==0){
            return Result.error("0","操作失败");
        }
        return Result.success();
    }


    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<VaccineOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.like(VaccineOrder::getDeleteFlag,0);
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(VaccineOrder::getOrderNo, search);
        }
        Page<VaccineOrder> orderPage = vaccineOrderMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(orderPage);
    }

}
